﻿package com.game.ctrler
{
    import base.BaseHero;
    
    import com.game.engine.MainGame;
    import com.game.manager.GameSceneManager;
    import com.game.manager.SettingManager;
    import com.game.view.component.StopPoint;
    import com.greensock.TweenMax;
    import com.greensock.easing.Quad;
    
    import config.Config;
    
    import flash.display.Sprite;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    
    public class BattleCamera
    {
        
        public function BattleCamera()
        {
            super();
            maxRightX = 940 - gc.gameScene.width;
            maxBGRightX = 940 - gc.floorBackGround.width;
            minGameSceneY = gc.gameScene.getBounds(gc.gameScene.parent).y;
        }
        
        private function get gc():Config
        {
            return Config.getIns();
        }
        private var role1:BaseHero;
        private var maxRightX:Number;
        private var maxBGRightX:Number;
        private var canBackward:Number = 0;
        private var thisStopX:Number = 0;
        private var lastPointX:Number = 0;
        //当杀完一波怪物后设置为true，会将镜头自动向前移动
        private var isAutoCamera:Boolean = false;
        private var shakeVal:Number = 0;
        private var _isStop:Boolean = false;
        private var minGameSceneY:Number = 0;
        
        public function shake(value:int):void
        {
            if(!SettingManager.getSettingVO().isCamaraShake)
            {
                return;
            }
            if (shakeVal == 0)
            {
                shakeVal = value;
            }
        }
        
        public function shine(color:uint = 0xFFFFFF, time:Number = 0.2):void
        {
            if(!SettingManager.getSettingVO().isScreenShine)
            {
                return;
            }
            var flash:Sprite = new Sprite();
            flash.graphics.beginFill(color);
            flash.graphics.drawRect(0, 0, GameSceneManager.getIns().mainStage.stageWidth, GameSceneManager.getIns().mainStage.stageHeight);
            flash.graphics.endFill();
            flash.alpha = 0.2;
            GameSceneManager.getIns().addScene(flash);
            TweenMax.to(flash, time, {
                alpha: 0,
                ease: Quad.easeOut,
                onComplete: function ():void
                {
                    if (flash.parent)
                    {
                        flash.parent.removeChild(flash);
                    }
                }
            })
        }
        
        public function render(ratio:Number):void
        {
            if (_isStop)
            {
                return;
            }
            if(!role1 || role1.isDead())
            {
                return;
            }
            if (MainGame.getIns().levelData.cameraMode == "horizontal" || MainGame.getIns().levelData.cameraMode == "free")
            {
                // 获取角色碰撞区域
                var collideArea1:Rectangle = role1.getDefiniteCollideArea();
                
                // 获取停止点数组的第一个元素
                var lastPointX:Number = 0;
                if (gc.pWorld.getStopPointArray().length > 0)
                {
                    var currentStopPoint:StopPoint = gc.pWorld.getStopPointArray()[0] as StopPoint;
                    if (lastPointX == 0)
                    {
                        var lastStopPoint:StopPoint = gc.pWorld.getStopPointArray()[gc.pWorld.getStopPointArray().length - 1] as StopPoint;
                        lastPointX = lastStopPoint.getDataX();
                    }
                }
                //stopPoint的全局坐标
                var globalStopPointX:Number = currentStopPoint ? gc.gameScene.localToGlobal(new Point(currentStopPoint.getDataX(), currentStopPoint.y)).x : -lastPointX + 870;
                
                var speed:Number = Math.max(getMaxSpeedBetweenToPlayer(), 0);
                var floorSpeed:Number = speed * (gc.floorBackGround.width - 940) / (gc.gameScene.width - 940);
                
                // 处理横向移动逻辑
                if (collideArea1.x >= 940 * 2 / 3 && collideArea1.x >= 940 / 5)
                {
                    if (thisStopX != 0 && gc.gameScene.x > thisStopX)
                    {
                        //已经触发了一个停止点
                        gc.gameScene.x -= speed / ratio;
                        gc.floorBackGround.x -= floorSpeed / ratio;
                        if (canBackward != -99)
                        {
                            canBackward += speed / ratio;
                        }
                    }
                    else if (currentStopPoint && !currentStopPoint.isSendMonster)
                    {
                        //前方存在停止点，且这个停止点未被激活，控制视角左移
                        gc.gameScene.x -= speed / ratio;
                        gc.floorBackGround.x -= floorSpeed / ratio;
                        canBackward += speed / ratio;
                        if (globalStopPointX <= 960 && globalStopPointX >= 15 * 60)
                        {
                            canBackward = currentStopPoint.getLeftDataX();
                            thisStopX = gc.gameScene.x;
                            currentStopPoint.touch();
                        }
                        else
                        {
                            canBackward = -99;
                            thisStopX = 0;
                        }
                    }
                }
                else if (collideArea1.x <= 940 / 5 && collideArea1.x <= 940 * 2 / 3)
                {
                    if (canBackward > 0 || canBackward == -99)
                    {
                        speed = Math.min(getMinSpeedBetweenToPlayer(), 0);
                        floorSpeed = speed * (gc.floorBackGround.width - 940) / (gc.gameScene.width - 940);
                        if (gc.gameScene.x - speed / ratio <= 0)
                        {
                            gc.gameScene.x -= speed / ratio;
                            gc.floorBackGround.x -= floorSpeed / ratio;
                            if (gc.floorBackGround.x > 0)
                            {
                                gc.floorBackGround.x = 0;
                            }
                            if (canBackward > 0)
                            {
                                canBackward -= -speed / ratio;
                            }
                        }
                    }
                }
            }
            
            if (MainGame.getIns().levelData.cameraMode == "vertical" || MainGame.getIns().levelData.cameraMode == "free")
            {
                // 计算所有英雄的平均Y坐标
                var averageHeroY:Number = 0;
                for each (var hero:BaseHero in gc.team.getHeroArray())
                {
                    averageHeroY += hero.y;
                }
                averageHeroY /= gc.team.getHeroArray().length;
                
                if (averageHeroY < 245)
                {
                    gc.gameScene.y = 245 - averageHeroY;
                }
                else if (averageHeroY > 580)
                {
                    // Additional logic if needed
                }
                if (minGameSceneY < 0 && gc.gameScene.y > -minGameSceneY)
                {
                    gc.gameScene.y = -minGameSceneY;
                }
            }
            
            if (shakeVal == 0 && isAutoCamera && currentStopPoint)
            {
                //当杀完一波怪物之后，如果人物的位置在
                var heroGlobalX:Number = gc.team.hero ? gc.team.hero.x : 99999;
                var heroSpeed:int = gc.team.hero ? Math.abs(gc.team.hero.velocity.x) : 0;
                var maxSpeed:int = 0;
                
                if (gc.gameScene.localToGlobal(new Point(heroGlobalX, 0)).x > 450)
                {
                    speed = Math.min(Math.max(heroSpeed, maxSpeed) + 10, 30);
                    gc.gameScene.x -= speed / ratio;
                    floorSpeed = speed * (gc.floorBackGround.width - 940) / (gc.gameScene.width - 940);
                    gc.floorBackGround.x -= floorSpeed / speed;
                }
                else
                {
                    isAutoCamera = false;
                }
                /*if (globalStopPointX <= 1000 && globalStopPointX >= 880)
                {
                    isAutoCamera = false;
                }*/
            }
        }
        
        public function renderAnimate():void
        {
            if (_isStop)
            {
                return;
            }
            if (shakeVal != 0)
            {
                gc.gameScene.x += shakeVal;
                shakeVal = shakeVal > 0 ? -shakeVal : 0;
            }
        }
        
        public function step():void
        {
            /*var _loc1_:* = null;
            var _loc2_:* = null;
            var _loc3_:* = null;
            var _loc7_:* = NaN;
            var _loc8_:Number = Number(NaN);
            var _loc9_:Number = Number(NaN);
            var _loc10_:* = null;
            var _loc11_:* = NaN;
            var _loc12_:* = NaN;
            var _loc13_:* = null;
            var _loc14_:Number = Number(NaN);
            var _loc15_:Number = Number(NaN);
            var _loc16_:int = 0;
            var _loc17_:int = 0;
            if (shakeVal > 0)
            {
                gc.gameScene.x += shakeVal;
                shakeVal *= -1;
            }
            else if (shakeVal < 0)
            {
                gc.gameScene.x += shakeVal;
                shakeVal = 0;
            }
            if (_isStop)
            {
                return;
            }
            if (MainGame.getIns().levelData.cameraMode == "horizontal" || MainGame.getIns().levelData.cameraMode == "free")
            {
                if (Boolean(role1) && !role1.isDead())
                {
                    _loc1_ = role1.getCollideArea();
                    _loc2_ = _loc1_;
                }
                if ((gc.pWorld.getStopPointArray() as Array).length > 0)
                {
                    _loc3_ = gc.pWorld.getStopPointArray()[0] as StopPoint;
                    if (lastPointX == 0)
                    {
                        _loc10_ = (gc.pWorld.getStopPointArray() as Array)[(gc.pWorld.getStopPointArray() as Array).length - 1] as StopPoint;
                        lastPointX = _loc10_.getDataX();
                    }
                }
                _loc7_ = 0;
                if (_loc3_)
                {
                    _loc9_ = Number(gc.gameScene.localToGlobal(new Point(_loc3_.getDataX(), _loc3_.y)).x);
                }
                else
                {
                    thisStopX = -Number(lastPointX) + 870;
                }
                ++count;
                if (_loc1_.x >= 940 * 2 / 3 && _loc2_.x >= 940 / 5)
                {
                    _loc8_ = (_loc7_ = Number(Math.max(getMaxSpeedBetweenToPlayer(), 0))) * (Number(gc.floorBackGround.width) - 940) / (Number(gc.gameScene.width) - 940);
                    if (thisStopX != 0 && gc.gameScene.x > thisStopX)
                    {
                        gc.gameScene.x -= _loc7_;
                        gc.floorBackGround.x -= _loc8_;
                        if (canBackward != -99)
                        {
                            canBackward += _loc7_;
                        }
                    }
                    else if (_loc3_ && !_loc3_.isSendMonster)
                    {
                        gc.gameScene.x -= _loc7_;
                        gc.floorBackGround.x -= _loc8_;
                        canBackward += _loc7_;
                        if (_loc9_ <= 980 && _loc9_ >= 15 * 60)
                        {
                            if (_loc3_)
                            {
                                canBackward = _loc3_.getLeftDataX();
                                thisStopX = gc.gameScene.x;
                                (_loc3_ as StopPoint).touch();
                            }
                        }
                        else
                        {
                            canBackward = -99;
                            thisStopX = 0;
                        }
                    }
                }
                else if (_loc1_.x >= 940 / 5 && _loc2_.x >= 940 * 2 / 3)
                {
                    _loc8_ = (_loc7_ = Number(Math.max(getMaxSpeedBetweenToPlayer(), 0))) * (Number(gc.floorBackGround.width) - 940) / (Number(gc.gameScene.width) - 940);
                    if (thisStopX != 0 && gc.gameScene.x > thisStopX)
                    {
                        gc.gameScene.x -= _loc7_;
                        gc.floorBackGround.x -= _loc8_;
                        if (canBackward != -99)
                        {
                            canBackward += _loc7_;
                        }
                    }
                    else if (_loc3_ && !_loc3_.isSendMonster)
                    {
                        gc.gameScene.x -= _loc7_;
                        gc.floorBackGround.x -= _loc8_;
                        canBackward += _loc7_;
                        if (_loc9_ <= 980 && _loc9_ >= 15 * 60)
                        {
                            if (_loc3_)
                            {
                                canBackward = _loc3_.getLeftDataX();
                                thisStopX = gc.gameScene.x;
                                _loc3_.touch();
                            }
                        }
                        else
                        {
                            canBackward = -99;
                            thisStopX = 0;
                        }
                    }
                }
                else if (_loc1_.x <= 940 * 2 / 3 && _loc2_.x <= 940 / 5 && (canBackward > 0 || canBackward == -99))
                {
                    if (canBackward > 0 || canBackward == -99)
                    {
                        _loc8_ = (_loc7_ = Number(Math.min(getMinSpeedBetweenToPlayer(), 0))) * (Number(gc.floorBackGround.width) - 940) / (Number(gc.gameScene.width) - 940);
                        if (Number(gc.gameScene.x) - _loc7_ <= 0)
                        {
                            gc.gameScene.x -= _loc7_;
                            gc.floorBackGround.x -= _loc8_;
                            if (gc.floorBackGround.x > 0)
                            {
                                gc.floorBackGround.x = 0;
                            }
                            if (canBackward > 0)
                            {
                                canBackward -= -_loc7_;
                            }
                        }
                    }
                }
                else if (_loc1_.x <= 940 / 5 && _loc2_.x <= 940 * 2 / 3 && (canBackward > 0 || canBackward == -99))
                {
                    if (canBackward > 0 || canBackward == -99)
                    {
                        _loc8_ = (_loc7_ = Number(Math.min(getMinSpeedBetweenToPlayer(), 0))) * (Number(gc.floorBackGround.width) - 940) / (Number(gc.gameScene.width) - 940);
                        if (Number(gc.gameScene.x) - _loc7_ <= 0)
                        {
                            gc.gameScene.x -= _loc7_;
                            gc.floorBackGround.x -= _loc8_;
                            if (gc.floorBackGround.x > 0)
                            {
                                gc.floorBackGround.x = 0;
                            }
                            if (canBackward > 0)
                            {
                                canBackward -= -_loc7_;
                            }
                        }
                    }
                }
            }
            if (MainGame.getIns().levelData.cameraMode == "vertical" || MainGame.getIns().levelData.cameraMode == "free")
            {
                _loc11_ = 0;
                for each (_loc13_ in gc.team.getHeroArray())
                {
                    _loc11_ = Number(_loc11_ + _loc13_.y);
                }
                if ((_loc11_ = Number(_loc11_ / Number(gc.team.getHeroArray().length))) < 245)
                {
                    gc.gameScene.y = 245 - _loc11_;
                }
                else if (_loc11_ > 580)
                {
                }
                if (minGameSceneY < 0)
                {
                    if (gc.gameScene.y > -Number(minGameSceneY))
                    {
                        gc.gameScene.y = -Number(minGameSceneY);
                    }
                }
            }
            if (shakeVal == 0)
            {
                if (Boolean(isAutoCamera) && _loc3_)
                {
                    _loc14_ = gc.team.hero != null ? gc.team.hero.x : Number(99999);
                    if ((_loc15_ = Number(gc.gameScene.localToGlobal(new Point(_loc14_, 0)).x)) > 450)
                    {
                        _loc16_ = !!gc.team.hero ? Math.abs(gc.team.hero.speed.x) : 0;
                        _loc17_ = 0;
                        if (Math.max(_loc16_, _loc17_) + 10 <= 30)
                        {
                            _loc7_ = Number(Math.max(_loc16_, _loc17_) + 10);
                        }
                        else
                        {
                            _loc7_ = 30;
                        }
                        gc.gameScene.x -= _loc7_;
                        _loc8_ = _loc7_ * (Number(gc.floorBackGround.width) - 940) / (Number(gc.gameScene.width) - 940);
                        gc.floorBackGround.x -= _loc8_;
                    }
                    else
                    {
                        isAutoCamera = false;
                    }
                    if (_loc9_ <= 1000 && _loc9_ >= 880)
                    {
                        isAutoCamera = false;
                    }
                }
            }*/
        }
        
        public function setAutoCamera():void
        {
            isAutoCamera = true;
        }
        
        public function stop():void
        {
            _isStop = true;
        }
        
        public function destroy():void
        {
            role1 = null;
            isAutoCamera = false;
        }
        
        public function setRole1(bh:BaseHero):void
        {
            role1 = bh;
        }
        
        private function getMaxSpeedBetweenToPlayer():Number
        {
            var s:Number = -99;
            if (role1 && !role1.isDead())
            {
                s = role1.velocity.x;
            }
            if (MainGame.getIns().isInSea())
            {
                s = s / 2;
            }
            return s;
        }
        
        private function getMinSpeedBetweenToPlayer():Number
        {
            var s:Number = 99;
            if (role1 && !role1.isDead())
            {
                s = role1.velocity.x;
            }
            if (s > 10)
            {
                s = 10;
            }
            if (MainGame.getIns().isInSea())
            {
                s = s / 2;
            }
            return s;
        }
    }
}
